package com.moyun.controller.upload;

import com.moyun.common.annotation.OperLog;
import com.moyun.common.aspect.BusinessType;
import com.moyun.controller.common.CommonController;
import com.moyun.model.common.R;
import com.moyun.model.po.upload.SysAttachmentEntity;
import com.moyun.service.upload.SysAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yangboxuan
 * @since 2021-07-13
 */
@Api(tags = "附件列表接口")
@RestController
@RequestMapping("/sys/file")
public class SysAttachmentController extends CommonController<Object, SysAttachmentEntity, Object, String> {

    @Autowired
    private SysAttachmentService sysAttachmentEntityService;

    /**
     * 上传文件，支持批量
     *
     * @param files   文件数据
     * @param docunid 所关联的文档id,如果为null则会随机生成一个uuid
     * @return 返回docunid值
     */
    @ApiOperation("文件上传")
    @PostMapping("upload")
    @OperLog(operModul = "附件列表模块", operType = BusinessType.OTHER, operDesc = "文件上传")
    public R save(@RequestParam("file") MultipartFile[] files,
                  @RequestParam(value = "docunid", required = false) String docunid,
                  @RequestParam(value = "attType", required = false) String attType,
                  @RequestParam(value = "width", required = false) Integer width,
                  @RequestParam(value = "height", required = false) Integer height) throws IOException {

        boolean flag = this.sysAttachmentEntityService.upload(files, docunid, attType, width, height);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * @return com.moyun.common.web.JsonResult
     * @Author yangboxuan
     * @Description 文件下载
     * @Date 2021/7/21 .
     * @Param []
     **/
    @ApiOperation("文件下载")
    @GetMapping("download")
    @OperLog(operModul = "附件列表模块", operType = BusinessType.OTHER, operDesc = "文件下载")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) throws IOException {
        // 目前实现: 根据传入的文件名,进行下载
        this.sysAttachmentEntityService.download(fileName, response);

    }
}

